# 智能客服系统 MVC前端启动脚本
Write-Host "=== 智能客服系统 MVC前端启动脚本 ===" -ForegroundColor Green

# 检查.NET环境
Write-Host "`n1. 检查.NET环境..." -ForegroundColor Yellow
try {
    $dotnetVersion = dotnet --version
    Write-Host "✓ .NET版本: $dotnetVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 未找到.NET SDK，请先安装.NET 8.0或更高版本" -ForegroundColor Red
    exit 1
}

# 检查项目文件
Write-Host "`n2. 检查项目文件..." -ForegroundColor Yellow
if (Test-Path "Intelli.Cust.Service.mvc.csproj") {
    Write-Host "✓ 项目文件存在" -ForegroundColor Green
} else {
    Write-Host "✗ 项目文件不存在，请确保在正确的目录中运行脚本" -ForegroundColor Red
    exit 1
}

# 还原依赖包
Write-Host "`n3. 还原依赖包..." -ForegroundColor Yellow
try {
    dotnet restore
    Write-Host "✓ 依赖包还原成功" -ForegroundColor Green
} catch {
    Write-Host "✗ 依赖包还原失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 构建项目
Write-Host "`n4. 构建项目..." -ForegroundColor Yellow
try {
    dotnet build --no-restore
    Write-Host "✓ 项目构建成功" -ForegroundColor Green
} catch {
    Write-Host "✗ 项目构建失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 检查后端API服务
Write-Host "`n5. 检查后端API服务..." -ForegroundColor Yellow
$apiUrl = "http://localhost:5035/health"
try {
    $response = Invoke-WebRequest -Uri $apiUrl -Method Get -TimeoutSec 5
    if ($response.StatusCode -eq 200) {
        Write-Host "✓ 后端API服务正常运行" -ForegroundColor Green
    } else {
        Write-Host "⚠ 后端API服务响应异常: $($response.StatusCode)" -ForegroundColor Yellow
    }
} catch {
    Write-Host "⚠ 后端API服务未运行或无法访问" -ForegroundColor Yellow
    Write-Host "  请确保后端服务在端口5035上运行" -ForegroundColor Cyan
    Write-Host "  或者修改appsettings.json中的API地址" -ForegroundColor Cyan
}

# 启动MVC应用
Write-Host "`n6. 启动MVC应用..." -ForegroundColor Yellow
Write-Host "  应用将在以下地址启动:" -ForegroundColor Cyan
Write-Host "  - 首页: http://localhost:5000" -ForegroundColor Cyan
Write-Host "  - 智能客服: http://localhost:5000/Chat" -ForegroundColor Cyan
Write-Host "  - 知识库: http://localhost:5000/Chat/Knowledge" -ForegroundColor Cyan
Write-Host "  - 关于: http://localhost:5000/Home/About" -ForegroundColor Cyan
Write-Host "  - 联系: http://localhost:5000/Home/Contact" -ForegroundColor Cyan

Write-Host "`n按 Ctrl+C 停止应用" -ForegroundColor Yellow
Write-Host "正在启动应用..." -ForegroundColor Green

try {
    dotnet run
} catch {
    Write-Host "`n✗ 应用启动失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "请检查错误信息并重试" -ForegroundColor Yellow
} 